#include "StdAfx.h"
#include "GameManager.h"

#include "car.h"
#include "particles.h"

#include <sstream> //for debug, maybe?

CGameManager::CGameManager(void)
{
	//setup the gameobject ID variable
	NextGameObjectID = 1;

	_deltaTime = 0;
	_timePassed = 0;
	_lastTickCount = 0;
	_parsingTime = 0;


	_flength = 20;
	_fwidth = 20;
	_blength = 4;
	_bwidth = 4;
	_desertFloor.LoadFile("images/desert.bmp");
	_base.LoadFile("images/base.bmp");

	//set up our car manager
	_cars.setSpawnDuration(0.5);
	_cars.setBounds(0, 0, 20, 20);
	AddObject(&_cars);

}

CGameManager::~CGameManager(void)
{
	_objects.clear();
}

void CGameManager::Update()
{
	UpdateTimeValues();

	for (GameObjectPtr gop = _objects.begin(); gop != _objects.end(); gop++)
	{
		(*gop)->Update(_deltaTime);
	}
}

void CGameManager::Render()
{
	DrawFloor();

	for (GameObjectPtr gop = _objects.begin(); gop != _objects.end(); gop++)
	{
		(*gop)->Render();
	}
}

void CGameManager::UpdateTimeValues()
{
	long ticks = GetCurrentTime();
	if (_lastTickCount == 0) _lastTickCount = ticks;
	_deltaTime = (ticks - _lastTickCount) / 1000.0;
	_timePassed += _deltaTime;
	_lastTickCount = ticks;
	
	
	//std::wstringstream wss;
	//wss << _deltaTime << " + time: " << _timePassed << " ticks: " << _lastTickCount;
	//OutputDebugString(wss.str().c_str());
	//OutputDebugString(L"\n");
} 

void CGameManager::DrawFloor()
{
        //Desert
        glEnable(GL_TEXTURE_2D);
        glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
        glBindTexture(GL_TEXTURE_2D, _desertFloor.TexName());

        glColor3d(1, 1, 1);
        glBegin(GL_POLYGON);
        glNormal3d(0., 1., 0.);
        glTexCoord2d(0, .8);
        glVertex3d(0., 0, _bwidth);
        glTexCoord2d(0, 0);
        glVertex3d(0., 0, _flength);
        glTexCoord2d(1, 0);
        glVertex3d(_fwidth, 0., _flength);
        glTexCoord2d(1., .8);
        glVertex3d(_fwidth, 0., _blength);
        glEnd();

        glColor3d(1, 1, 1);
        glBegin(GL_POLYGON);
        glNormal3d(0., 1., 0.);
        glTexCoord2d(.2, 1.);
        glVertex3d(_blength, 0., 0.);
        glTexCoord2d(.2, .8);
        glVertex3d(_blength, 0., _bwidth);
        glTexCoord2d(1, .8);
        glVertex3d(_flength, 0., _bwidth);
        glTexCoord2d(1., 1.);
        glVertex3d(_flength, 0., 0.);
        glEnd();

        //Base
        glEnable(GL_TEXTURE_2D);
        glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
        glBindTexture(GL_TEXTURE_2D, _base.TexName());

        glColor3d(1, 1, 1);
        glBegin(GL_POLYGON);
        glNormal3d(0., 1., 0.);
        glTexCoord2d(0, 0);
        glVertex3d(0, 0, 0);
        glTexCoord2d(1, 0);
        glVertex3d(0, 0, _bwidth);
        glTexCoord2d(1, 1);
        glVertex3d(_blength, 0., _bwidth);
        glTexCoord2d(0, 1);
        glVertex3d(_blength, 0., 0.);
        glEnd();

		glDisable(GL_TEXTURE_2D);
}

void CGameManager::Initialize()
{
	
}

void CGameManager::AddObject(CGameComponent *gc)
{
	gc->ID(NextGameObjectID);
	NextGameObjectID++;
	_objects.push_back(gc);
}

bool CGameManager::RemoveObject(unsigned int ID)
{
	GameObjectPtr torem = _objects.end();
	for (GameObjectPtr gear = _objects.begin(); gear != _objects.end(); gear++)
	{
		if ((*gear)->ID() == ID)
		{
			torem = gear;
			break;
		}
	}

	if (torem != _objects.end()) //we found it
	{
		_objects.erase(torem);
		return true;
	}
	return false; //did not find the object
}

void CGameManager::CheckCollision(CHelicopter* heli)
{
	_cars.checkCarCollisionWithPlayer(heli);
}
